package com.ruoyi.web.controller.wnjk.app.shop;

import com.alipay.api.domain.StockTaskResult;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.wnjk.domain.dto.CartItem;
import com.ruoyi.system.wnjk.domain.dto.StockCheckResult;
import com.ruoyi.system.wnjk.domain.po.ShopItem;
import com.ruoyi.system.wnjk.service.AppCartService;
import com.ruoyi.system.wnjk.service.IShopItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/app/api/cart")
public class AppCartController {

    @Autowired
    private AppCartService cartService;

    // 添加商品到购物车
    @PostMapping("/add")
    public AjaxResult addToCart(@RequestParam String userId, @RequestBody CartItem cartItem) {
        cartService.addToCart(userId, cartItem);
        return AjaxResult.success("添加到redis",null);
    }

    // 获取用户的购物车数据
    @GetMapping("/get")
    public AjaxResult getCart(@RequestParam String userId) {
        Map<String, CartItem> cart = cartService.getCart(userId);
        return AjaxResult.success(cart);
    }

    // 删除购物车商品
    @PostMapping("/delete")
    public void deleteCartItem(
            @RequestParam String userId,
            @RequestBody CartItem cartItem) {
        cartService.removeFromCart(userId, cartItem.getId());
    }

    //批量删除
    @PostMapping("/deleteBatch")
    public ResponseEntity<?> deleteCartItems(@RequestBody Map<String, Object> request) {
        String userId = request.get("userId").toString();
        List<String> itemIds = (List<String>) request.get("itemIds");

        try {
            cartService.deleteCartItems(userId, itemIds); // 调用服务类删除 Redis 中的数据
            return ResponseEntity.ok().body("删除成功");
        } catch (Exception e) {
            return ResponseEntity.status(500).body("删除失败");
        }
    }

    // 清空购物车
    @PostMapping("/clear")
    public void clearCart(@RequestParam String userId) {
        cartService.clearCart(userId);
    }

    @Autowired
    private IShopItemService shopItemService;

    //查看库存，返回一个数组
    @PostMapping("/checkStock")
    public ResponseEntity<?> checkStock(@RequestBody List<CartItem> cartItems) {
        List<StockCheckResult> results = new ArrayList<>();
        boolean isStockEnough = true;
        String message = "库存充足";

        for (CartItem item : cartItems) {
            Long id = Long.valueOf(item.getId());
            ShopItem shopItem = shopItemService.getStockById(id); // 从数据库查询库存
            int stock = shopItem.getStock();
            boolean isEnough = item.getQuantity() <= stock;
            results.add(new StockCheckResult(item.getId(), stock, isEnough));

            if (!isEnough) {
                isStockEnough = false;
                message = "库存不足";
            }
        }

        Map<String, Object> response = new HashMap<>();
        response.put("success", isStockEnough);
        response.put("message", message);
        response.put("data", results);

        return ResponseEntity.ok(response);
    }

}
